#include<iostream>
#include<vector>
#include<queue>
using namespace std;

int dx[4] = { 0, 0, 1, -1 };
int dy[4] = { 1, -1, 0, 0 };
int n, m;
queue<pair<int, int>> q;

int bfs(vector<vector<int> >& matrix) {
    vector<vector<bool>>vis(n,vector<bool>(m));
    int step = 0;
    while (q.size()) {
        int t = q.size();
        step++;
        while (t--) {
            auto p = q.front();
            int a = p.first, b = p.second;
            vis[a][b] = true;
            q.pop();
            for (int i = 0; i < 4; i++) {
                int x = a + dx[i], y = b + dy[i];
                if (x >= 0 && x < n && y >= 0 && y < m && !vis[x][y] && matrix[a][b] < matrix[x][y]) {
                    q.push({ x, y });
                }
            }
        }
    }
    return step;
}

int solve(vector<vector<int> >& matrix) {
    n = matrix.size(), m = matrix[0].size();
    int _max = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            q.push({ i, j });
            _max = max(bfs(matrix), _max);
        }
    }
    return _max;
}



int main()
{
    vector<vector<int>> matrix = { {1,2,3},{4,5,6},{7,8,9} };
    solve(matrix);
    return 0;
}